package com.lm.activiti.boot.cases;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.web.context.ContextLoader;

/**
 * @author lm_jcf
 * @date 2020/6/15 14:19
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({"classpath*:applicationContext.xml"})
@Component
public class AspectDemo {

    public static final Logger LOG = LoggerFactory.getLogger(AspectDemo.class);

    @Autowired
    private Play play;

    @Test
    public void test(){

//        LOG.info("存在：" + ContextLoader.getCurrentWebApplicationContext().containsBean("play"));

//        Play play = ContextLoader.getCurrentWebApplicationContext().getBean(Play.class);

        LOG.info(play.playGames("天谕"));

        play.start();
    }
}

@Component
class Play{

    public static final Logger LOG = LoggerFactory.getLogger(Play.class);

    public String playGames(String gameName) {

        return "先下载游戏：" + gameName;
    }

    public void start(){
        LOG.info("游戏启动。。。。");
    }

}

@Aspect
class PlayAop {

    public static final Logger LOG = LoggerFactory.getLogger(PlayAop.class);

    /*@Before("execution(* com.lm.activiti.boot.cases.Play.playGames(..)) " +
            "|| execution(* com.lm.activiti.boot.cases.Play.start(..))")
    public void test(){
        LOG.info("切面。。。。。。。。。");
    }*/

    @Pointcut("execution(* com.lm.activiti.boot.cases.Play.playGames(..))")
    public void playAop() {

    }

    @AfterReturning(value = "playAop()", returning = "resp")
    public void playAopHandle(JoinPoint jp, Object resp) {

        LOG.info("进入后置增强 ");

        Object o = jp.getArgs()[0];

        LOG.info("参数：" + o);

        LOG.info("返回值：" + resp);
    }
}
